import pynput
from pynput.keyboard import Controller, Key

keyboard = Controller()
global x_e

global flag
flag = 1


def action1():
    keyboard.press(Key.cmd)  # windows下使用
    keyboard.press(Key.ctrl)
    keyboard.press(Key.right)
    keyboard.release(Key.cmd)
    keyboard.release(Key.ctrl)
    keyboard.release(Key.right)


def action_2():
    keyboard.press(Key.cmd)  # windows下使用
    keyboard.press(Key.ctrl)
    keyboard.press(Key.left)
    keyboard.release(Key.cmd)
    keyboard.release(Key.ctrl)
    keyboard.release(Key.left)


def listen():
    def on_click(x, y, button, pressed):
        # time.sleep(1)
        # print("Pointer moved to {0}".format((x, y)))
        # print(button)
        # print(pressed)
        global flag
        global x_e
        if flag == 1:
            x_e = x
            flag = 0
        if flag == 0:
            if (
                x_e - x > 800
                and x_e - x > 0
                and button.name == "right"
                and pressed == True
            ):
                # print("该动作被触发1")
                # listener.stop()
                action1()
                x_e = x
                # listener.start()/

            elif (
                x_e - x < -800
                and x_e - x < 0
                and button.name == "right"
                and pressed == True
            ):
                # print("该动作被触发")
                # listener.stop()
                action_2()
                x_e = x
                # listener.stop()
                # listener.start()/

    # Collect events until released
    with pynput.mouse.Listener(on_click=on_click) as listener:
        listener.join()


if __name__ == "__main__":
    listen()
